package jiang.web.controller.dataCollection;

import java.io.File;

import javax.servlet.http.HttpSession;

import jiang.app.entity.Candidate;
import jiang.app.service.UpdateInformationService;
import jiang.commom.DateHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UpdateInfomationController {

	@Autowired
	UpdateInformationService inforService;

	@RequestMapping(value = "manage/dataCollection/updateInformation", method = RequestMethod.GET)
	public String index() {
		return "dataCollection/updateInformation/index";
	}

	@RequestMapping(value = "manage/dataCollection/updateinformation/query", method = RequestMethod.POST)
	public String queryById(String IDCardNumber, Model m) {
		Candidate c = inforService.findCandidateByIDCard(IDCardNumber);
		m.addAttribute("candidate", c);
		return "dataCollection/updateInformation/query";
	}

	@RequestMapping(value = "manage/dataCollection/updateinformation/update", method = RequestMethod.POST)
	public String update(Candidate c, MultipartFile file, HttpSession s, Model m) {

		String savePath = s.getServletContext().getRealPath(
				"jsp/dataCollection/resume")
				+ File.separator
				+ c.getName()
				+ "-"
				+ DateHelper.getCurrentDate()
				+ "-"
				+ file.getOriginalFilename();
		//更新
		if (inforService.updateCandidate(c)
				&& inforService.saveResume(file, savePath)) {
			m.addAttribute("tip", "更新成功");
		}
		else{
			m.addAttribute("tip", "更新失败");
		}
		return "dataCollection/updateInformation/result";
	}
}
